﻿using Network.FrameSync;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Object = UnityEngine.Object;

public class XiangjiangManager : MonoBehaviour
{
    public List<VRPlayer> vRPlayers;
    private VRPlayer _self;

    private void Awake()
    {
        vRPlayers = new List<VRPlayer>();
    }

    private void Start()
    {
        Client.OnlineManager.Instance.Net.Rpc.RegisterListener(this);

        // 创建玩家
        createPlayers();

        Client.XJGameManager.Instance.Fsp.SetFrameListener(enterFrame);

        // 开启客户端控制
        Client.XJGameManager.Instance.ControlStart();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Keypad7))
        {
            Client.XJGameManager.Instance.RoundEnd();
        }
        if (Input.GetKeyDown(KeyCode.Keypad8))
        {
            Client.XJGameManager.Instance.GameEnd();
        }
        if (Input.GetKeyDown(KeyCode.Keypad9))
        {
            Client.XJGameManager.Instance.GameExit();
        }
    }

    private void FixedUpdate()
    {
        long nowticks = System.DateTime.Now.Ticks;
        long interval = nowticks - _lastTicks;
        if (interval > _frameInterval)
        {
            _lastTicks = nowticks;
            testSyncPosition();
        }
    }

    private void OnDestroy()
    {
        // 注销RPC监听
        Client.OnlineManager.Instance.Net.Rpc.UnRegisterListener(this);
    }



    private void createPlayers()
    {
        for (int i = 0; i < Client.XJGameManager.Instance.players.Count; i++)
        {
            PlayerData playerData = Client.XJGameManager.Instance.players[i];

            VRPlayer vRPlayer = new VRPlayer();

            GameObject prefab = null;
            Vector3 position = Vector3.zero;
            if (playerData.id == Client.OnlineManager.Instance.MainUserData.id)
            {
                prefab = Resources.Load<GameObject>("player1");

                _self = vRPlayer;
            }
            else
            {
                prefab = Resources.Load<GameObject>("player2");
            }

            if (playerData.id == 1)
            {
                position = new Vector3(3, 0, 0);
            }
            else
            {
                position = new Vector3(-3, 0, 0);
            }

            vRPlayer.entity = Object.Instantiate(prefab);
            vRPlayer.entity.name = "VRPlayer" + playerData.id;
            vRPlayer.entity.transform.Find("Text").GetComponent<TextMeshPro>().text = vRPlayer.entity.name;
            vRPlayer.entity.transform.position = position;
            vRPlayer.playerData = playerData;

            vRPlayers.Add(vRPlayer);
        }
    }


    private bool _head = true;
    private bool _leftArm = true;
    private bool _rightArm = true;
    private bool _leftLeg = true;
    private bool _rightLeg = true;
    private long _lastTicks = 0;
    private long _frameInterval = 33 * 10000;
    public float _smoothingDelay = 20;
    private void testSyncPosition()
    {
        if (Input.GetKeyDown(KeyCode.Keypad1))
        {
            _head = !_head;
            _self.Head.GetComponent<AIRandMove>().enabled = _head;
        }
        if (Input.GetKeyDown(KeyCode.Keypad2))
        {
            _leftArm = !_leftArm;
            _self.LeftArm.GetComponent<AIRandMove>().enabled = _leftArm;
        }
        if (Input.GetKeyDown(KeyCode.Keypad3))
        {
            _rightArm = !_rightArm;
            _self.RightArm.GetComponent<AIRandMove>().enabled = _rightArm;
        }
        if (Input.GetKeyDown(KeyCode.Keypad4))
        {
            _leftLeg = !_leftLeg;
            _self.LeftLeg.GetComponent<AIRandMove>().enabled = _leftLeg;
        }
        if (Input.GetKeyDown(KeyCode.Keypad5))
        {
            _rightLeg = !_rightLeg;
            _self.RightLeg.GetComponent<AIRandMove>().enabled = _rightLeg;
        }

        if (_head)
        {
            int p_x = (int)(_self.Head.position.x * 10000);
            int p_y = (int)(_self.Head.position.y * 10000);
            int p_z = (int)(_self.Head.position.z * 10000);
            int r_x = (int)(_self.Head.eulerAngles.x * 10000);
            int r_y = (int)(_self.Head.eulerAngles.y * 10000);
            int r_z = (int)(_self.Head.eulerAngles.z * 10000);

            Client.XJGameManager.Instance.Fsp.SendFSP(FSPXJCmd.PLAYER_HEAD, p_x, p_y, p_z, r_x, r_y, r_z);
        }

        if (_leftArm)
        {
            int p_x = (int)(_self.LeftArm.position.x * 10000);
            int p_y = (int)(_self.LeftArm.position.y * 10000);
            int p_z = (int)(_self.LeftArm.position.z * 10000);
            int r_x = (int)(_self.LeftArm.eulerAngles.x * 10000);
            int r_y = (int)(_self.LeftArm.eulerAngles.y * 10000);
            int r_z = (int)(_self.LeftArm.eulerAngles.z * 10000);

            Client.XJGameManager.Instance.Fsp.SendFSP(FSPXJCmd.PLAYER_LEFT_ARM, p_x, p_y, p_z, r_x, r_y, r_z);
        }

        if (_rightArm)
        {
            int p_x = (int)(_self.RightArm.position.x * 10000);
            int p_y = (int)(_self.RightArm.position.y * 10000);
            int p_z = (int)(_self.RightArm.position.z * 10000);
            int r_x = (int)(_self.RightArm.eulerAngles.x * 10000);
            int r_y = (int)(_self.RightArm.eulerAngles.y * 10000);
            int r_z = (int)(_self.RightArm.eulerAngles.z * 10000);

            Client.XJGameManager.Instance.Fsp.SendFSP(FSPXJCmd.PLAYER_RIGHT_ARM, p_x, p_y, p_z, r_x, r_y, r_z);
        }

        if (_leftLeg)
        {
            int p_x = (int)(_self.LeftLeg.position.x * 10000);
            int p_y = (int)(_self.LeftLeg.position.y * 10000);
            int p_z = (int)(_self.LeftLeg.position.z * 10000);
            int r_x = (int)(_self.LeftLeg.eulerAngles.x * 10000);
            int r_y = (int)(_self.LeftLeg.eulerAngles.y * 10000);
            int r_z = (int)(_self.LeftLeg.eulerAngles.z * 10000);

            Client.XJGameManager.Instance.Fsp.SendFSP(FSPXJCmd.PLAYER_LEFT_LEG, p_x, p_y, p_z, r_x, r_y, r_z);
        }

        if (_rightLeg)
        {
            int p_x = (int)(_self.RightLeg.position.x * 10000);
            int p_y = (int)(_self.RightLeg.position.y * 10000);
            int p_z = (int)(_self.RightLeg.position.z * 10000);
            int r_x = (int)(_self.RightLeg.eulerAngles.x * 10000);
            int r_y = (int)(_self.RightLeg.eulerAngles.y * 10000);
            int r_z = (int)(_self.RightLeg.eulerAngles.z * 10000);

            Client.XJGameManager.Instance.Fsp.SendFSP(FSPXJCmd.PLAYER_RIGHT_LEG, p_x, p_y, p_z, r_x, r_y, r_z);
        }


    }

    private void enterFrame(int frame, FSPFrame fSPFrame)
    {
        //UnityEngine.Debug.Log("当前帧:" + frame);

        if (fSPFrame == null || fSPFrame.msgs == null || fSPFrame.msgs.Count == 0) return;

        for (int i = 0; i < fSPFrame.msgs.Count; i++)
        {
            FSPMessage fSPMessage = fSPFrame.msgs[i];

            if (fSPMessage.playerId == _self.playerData.id) continue;

            for (int j = 0; j < vRPlayers.Count; j++)
            {
                VRPlayer vrPlayer = vRPlayers[j];
                if (vrPlayer.playerData.id == fSPMessage.playerId)
                {
                    if (fSPMessage.cmd == FSPXJCmd.PLAYER_HEAD)
                    {
                        float p_x = fSPMessage.args[0] / 10000f;
                        float p_y = fSPMessage.args[1] / 10000f;
                        float p_z = fSPMessage.args[2] / 10000f;
                        float r_x = fSPMessage.args[3] / 10000f;
                        float r_y = fSPMessage.args[4] / 10000f;
                        float r_z = fSPMessage.args[5] / 10000f;

                        vrPlayer.Head.position = Vector3.Lerp(vrPlayer.Head.position, new Vector3(p_x, p_y, p_z), Time.deltaTime * _smoothingDelay);
                        vrPlayer.Head.rotation = Quaternion.Lerp(vrPlayer.Head.rotation, Quaternion.Euler(r_x, r_y, r_z), Time.deltaTime * _smoothingDelay);

                        //vrPlayer.Head.position = new Vector3(p_x, p_y, p_z);
                        //vrPlayer.Head.eulerAngles = new Vector3(r_x, r_y, r_z);
                    }
                    if (fSPMessage.cmd == FSPXJCmd.PLAYER_LEFT_ARM)
                    {
                        float p_x = fSPMessage.args[0] / 10000f;
                        float p_y = fSPMessage.args[1] / 10000f;
                        float p_z = fSPMessage.args[2] / 10000f;
                        float r_x = fSPMessage.args[3] / 10000f;
                        float r_y = fSPMessage.args[4] / 10000f;
                        float r_z = fSPMessage.args[5] / 10000f;

                        vrPlayer.LeftArm.position = Vector3.Lerp(vrPlayer.LeftArm.position, new Vector3(p_x, p_y, p_z), Time.deltaTime * _smoothingDelay);
                        vrPlayer.LeftArm.rotation = Quaternion.Lerp(vrPlayer.LeftArm.rotation, Quaternion.Euler(r_x, r_y, r_z), Time.deltaTime * _smoothingDelay);

                        //vrPlayer.LeftArm.position = new Vector3(p_x, p_y, p_z);
                        //vrPlayer.LeftArm.eulerAngles = new Vector3(r_x, r_y, r_z);
                    }
                    if (fSPMessage.cmd == FSPXJCmd.PLAYER_RIGHT_ARM)
                    {
                        float p_x = fSPMessage.args[0] / 10000f;
                        float p_y = fSPMessage.args[1] / 10000f;
                        float p_z = fSPMessage.args[2] / 10000f;
                        float r_x = fSPMessage.args[3] / 10000f;
                        float r_y = fSPMessage.args[4] / 10000f;
                        float r_z = fSPMessage.args[5] / 10000f;

                        vrPlayer.RightArm.position = Vector3.Lerp(vrPlayer.RightArm.position, new Vector3(p_x, p_y, p_z), Time.deltaTime * _smoothingDelay);
                        vrPlayer.RightArm.rotation = Quaternion.Lerp(vrPlayer.RightArm.rotation, Quaternion.Euler(r_x, r_y, r_z), Time.deltaTime * _smoothingDelay);

                        //vrPlayer.RightArm.position = new Vector3(p_x, p_y, p_z);
                        //vrPlayer.RightArm.eulerAngles = new Vector3(r_x, r_y, r_z);
                    }
                    if (fSPMessage.cmd == FSPXJCmd.PLAYER_LEFT_LEG)
                    {
                        float p_x = fSPMessage.args[0] / 10000f;
                        float p_y = fSPMessage.args[1] / 10000f;
                        float p_z = fSPMessage.args[2] / 10000f;
                        float r_x = fSPMessage.args[3] / 10000f;
                        float r_y = fSPMessage.args[4] / 10000f;
                        float r_z = fSPMessage.args[5] / 10000f;

                        vrPlayer.LeftLeg.position = Vector3.Lerp(vrPlayer.LeftLeg.position, new Vector3(p_x, p_y, p_z), Time.deltaTime * _smoothingDelay);
                        vrPlayer.LeftLeg.rotation = Quaternion.Lerp(vrPlayer.LeftLeg.rotation, Quaternion.Euler(r_x, r_y, r_z), Time.deltaTime * _smoothingDelay);

                        //vrPlayer.LeftLeg.position = new Vector3(p_x, p_y, p_z);
                        //vrPlayer.LeftLeg.eulerAngles = new Vector3(r_x, r_y, r_z);
                    }
                    if (fSPMessage.cmd == FSPXJCmd.PLAYER_RIGHT_LEG)
                    {
                        float p_x = fSPMessage.args[0] / 10000f;
                        float p_y = fSPMessage.args[1] / 10000f;
                        float p_z = fSPMessage.args[2] / 10000f;
                        float r_x = fSPMessage.args[3] / 10000f;
                        float r_y = fSPMessage.args[4] / 10000f;
                        float r_z = fSPMessage.args[5] / 10000f;

                        vrPlayer.RightLeg.position = Vector3.Lerp(vrPlayer.RightLeg.position, new Vector3(p_x, p_y, p_z), Time.deltaTime * _smoothingDelay);
                        vrPlayer.RightLeg.rotation = Quaternion.Lerp(vrPlayer.RightLeg.rotation, Quaternion.Euler(r_x, r_y, r_z), Time.deltaTime * _smoothingDelay);

                        //vrPlayer.RightLeg.position = new Vector3(p_x, p_y, p_z);
                        //vrPlayer.RightLeg.eulerAngles = new Vector3(r_x, r_y, r_z);
                    }
                }
            }
        }
    }
}